package com.atguigu.url.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/16 13:04
 * @description:
 */
@Component
public class JsonReadFromLocalFile {

    @Autowired
    RegUtils regUtils;


    /**
     * json文件路径
     *
     * @param path
     * @return
     */
    @Deprecated
    public StringBuilder getJsonStrBuilderFromLocalJsonFile(String path) {
        System.out.println(" 执行 ==> getJsonStrBuilderFromLocalJsonFile(String path) ");

        File file = new File(path);
        BufferedReader reader = null;
        StringBuilder sb = null;

        try {

            // System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;

            // 一次读入一行，直到读入null为文件结束
            sb = new StringBuilder();
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString);
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            ResourcesCloseUtils.close(reader);
        }

        System.out.println("read json ok and return sb");
        return sb;
    }


    /**
     * 直接拿到目标, 不用浪费资源去转来转去
     *
     * @param path
     * @return
     */
    public Set<String> getUrlSetFromJsonByLine(String path) {
        System.out.println(" 执行 ==> getUrlSetFromJsonByLine(String path) ");

        File file = new File(path);
        BufferedReader reader = null;

        HashSet<String> urlSetCollection = null;
        try {

            // System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;

            // 一次读入一行，直到读入null为文件结束
            urlSetCollection = new HashSet<>();
            while ((tempString = reader.readLine()) != null) {
                //直接拆出url
                if (tempString.contains("url") && tempString.contains("https://") && tempString.contains("?a=")) {
                    Set<String> urlSet = regUtils.findUrlSetFromTextOrStr(tempString);
                    for (String url : urlSet) {
                        if (url.contains("https://") && url.contains("?a=")) {
                            urlSetCollection.add(url);
                        }
                    }
                }
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            ResourcesCloseUtils.close(reader);
        }

        // 看一下有多少个 url
        if (urlSetCollection != null) {
            int urlNums = urlSetCollection.size();
            System.out.println(" urlNums ==> " + urlNums);
        }

        return urlSetCollection;
    }


    /**
     * 解析json 获取 urlSet
     * 低效代码, 不建议使用, 如果是小文本还可以用用
     * 该方法可以进一步抽象封装成为一般的方法, 供给所有类似需求使用
     * todo
     * 只要把json模式指出来, 就可以完成一般的解析逻辑
     *          提供json模式  {_xxx_[_xxx_{_xxx_...
     *          提供你的目标字段 xxx xxx xxx
     *          或者帮你映射为指定类型 XxxType.class
     *          仅仅使用于小文件100M左右的大小, 超大文件不行大于200M文件就不适合
     *
     * @param path
     * @return
     */
    @Deprecated
    public HashSet<String> getUrlSetByFullJson(String path) {
        System.out.println(" 执行 ==> getUrlSetByFullJson(String path) ");
        //去重
        HashSet<String> urlSet = new HashSet<>();

//        String path = JsonTest.class.getClassLoader().getResource("resources/douyin.json").getPath();
        StringBuilder sb = getJsonStrBuilderFromLocalJsonFile(path);

//        StringBuilder sb = new StringBuilder();
//        JSONObject.parseObject(sb.toString());
        // sb.toString() 及其浪费时间和空间  return new String("xxx")
        JSONObject o1 = JSONObject.parseObject(sb.toString());
        //处理内存不需要的对象, 直接干掉
        sb = null;

        String str1 = o1.getString("events");

        JSONArray arr1 = JSONArray.parseArray(str1);
        str1 = null;

        for (Object o : arr1) {
//            System.out.println("o = " + o.getClass());

            if (o.toString().contains("\"params\"") && o instanceof JSONObject) {

                String paramsStr = ((JSONObject) o).getString("params");

                if (paramsStr.contains("\"url\"") && paramsStr.contains("https://") && paramsStr.contains("?a=")) {

                    JSONObject o2 = JSONObject.parseObject(paramsStr);
                    //清理垃圾
                    paramsStr = null;
                    String url = o2.getString("url");
//                    System.out.println("url = " + url);

                    urlSet.add(url);
                    url = null;
                }
            }
        }

        return urlSet;
    }



}

